﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Base
{
    class Package
    {
        //前缀 
        public static byte[] PREFIXS = { 0xFF, 0xFF };

        //长度
        public byte length;

        //地址信息
        public byte[] addrs = new byte[6];

        //类型数据
        public byte type = 0xBB;

        public byte flag = 0xBB;
        //命令码
        public byte c;
        //数据类型
        public byte dataType;

        //面板号
        public byte panelNo=0;

        //子序列号
        public byte subDataSN=0;

        //数据内容
        public byte[] data;

        // 校验和
        public byte cs;



        //产生包

        public static Package getPackage(byte c,byte dataType,byte panelNo, byte subDataSN,byte[] data)
        {
            Package package = new Package();
            package.c = c;
            package.dataType = dataType;
            package.data =  data;
            package.panelNo = panelNo;

            package.subDataSN = subDataSN;

            if (data != null)
            {
                package.length = (byte)(13 + data.Length);
            }

            package.cs = genCs(package);
            return  package;
           
        }

        public static byte[] packageToByte(Package package)
        {
            int length = 16;
            if (package.data != null)
            {
                int addlength = 0;//0xff
                foreach (byte addr in package.data)
                {
                    if (addr == 0xFF)
                    {
                        addlength++;
                    }
                }
                length = length + package.data.Length + addlength;
            }

            byte[] rt = new byte[length];
            int cur=0;
            rt[cur++] = PREFIXS[0];
            rt[cur++]  = PREFIXS[1];
            rt[cur++] = package.length;

            foreach (byte addr in package.addrs)
            {
                rt[cur++] = addr;
            }

            rt[cur++] = package.type;
            rt[cur++] = package.flag;
            rt[cur++] = package.c;
            rt[cur++] = package.dataType;
            rt[cur++] = package.panelNo;
            rt[cur++] = package.subDataSN;

            if (package.data != null)
            {
                foreach (byte d in package.data)
                {
                    rt[cur++] = d;
                    if (d == 0xff)
                    {
                        rt[cur++] = 0x55;
                    }
                  
                }
              
            }
            rt[cur++] = package.cs;

            return rt;
        }

        private static byte  genCs(Package package){

            int rt = 0;

            byte length = package.length;
          
            byte[] addrs = package.addrs;

            byte type = package.type;

            byte flag = package.flag;

            byte c = package.c;

            byte dataType = package.dataType;

            byte panelNo = package.panelNo;

            byte subDataSN = package.subDataSN;

            byte[] data = package.data;


            rt = rt + length;

            foreach(byte addr in addrs ){

                rt = rt + addr;
                
            }
            rt = rt + type;
            rt = rt + flag;
            rt = rt + c;
            rt = rt + dataType;
            rt = rt + panelNo;
            rt = rt + subDataSN;

            if (data != null)
            {
                foreach (byte d in data)
                {
                    rt = rt + d;
                }

            }


            return (byte)(rt % 256);

        }
    }
}
